<template>
  <div class="shopcart">
    <div class="content" @click="toggleList">
      <div class="content-left">
        <div class="icon-wrap" :class="{'highlight' : totalCount > 0}">
          <div class="icon" :class="{'highlight' : totalCount > 0}">
            <i class="icon-shopping_cart"></i>
          </div>
          <div v-show="totalCount > 0" class="cart-num">{{ totalCount }}</div>
        </div>
        <div class="price" :class="{'highlight' : totalPrice > 0}">￥{{ totalPrice }}</div>
        <div class="desc">另需配送费￥{{ deliveryPrice }}</div>
      </div>
      <div class="content-right" @click.stop.prevent="checkout">
        <div :class="payClass" class="pay">{{ payDesc }}</div>
      </div>
    </div>
    <!-- 小球 -->
    <div class="ball-container">
      <div transition="drop" v-for="ball in balls" v-show="ball.show" class="ball">
        <div class="inner inner-hook"></div>
      </div>
    </div>
    <!-- 购物车 -->
    <div class="shopcart-list" v-show="listShow" transition="fold">
      <div class="list-header">
        <h2 class="title">购物车</h2>
        <span class="empty" @click="empty">清空</span>
      </div>
      <div class="list-content" v-el:list-content>
        <ul>
          <li v-for="food in selectFoods" class="food">
            <span class="name">{{ food.name }}</span>
            <div class="price">
              <span>￥{{ food.price * food.count }}</span>
            </div>
            <div class="cart-ctrl-wrapper">
              <controll :food="food"></controll>
            </div>
          </li>
        </ul>
      </div>
    </div>
  </div>
  <div class="list-mask" v-show="listShow" @click="hideList" transition="fade"></div>
</template>
<script>
  import BScroll from 'better-scroll'
  import Controll from 'components/cartcontroll/cartcontroll'
  export default {
    props: {
      selectFoods: {
        type: Array,
        default() {
          return [{
            price: 10,
            count: 1
          }]
        }
      },
      deliveryPrice: {
        type: Number
      },
      minPrice: {
        type: Number
      }
    },
    data() {
      return {
        balls: [
          {show: false},
          {show: false},
          {show: false},
          {show: false},
          {show: false}
        ],
        dropBalls: [], // 已经落下的小球
        fold: true
      }
    },
    computed: {
      totalPrice() {
        let total = 0
        this.selectFoods.forEach((food) => {
          total += food.price * food.count
        })
        return total
      },
      totalCount() {
        let count = 0
        this.selectFoods.forEach((food) => {
          count += food.count
        })
        return count
      },
      payDesc() {
        if(this.totalPrice === 0) {
          return `￥${this.minPrice}元起送`
        }
        else if(this.totalPrice < this.minPrice) {
          let diff = this.minPrice - this.totalPrice
          return `还差￥${diff}元送`
        }
        else{
          return `去结算`
        }
      },
      payClass() {
        if(this.totalPrice >= this.minPrice) {
          return 'enough'
        }
      },
      listShow() {
        if(!this.totalCount) {
          this.fold = true
          return false
        }
        let show = !this.fold
        if(show) {
          this.$nextTick(() => {
            if(!this.scroll) {
              this.scroll = new BScroll(this.$els.listContent,{
                click: true
              })
            }
            else {
              this.scroll.refresh()
            }
          })
        }
        return show
      }
    },
    methods: {
      drop(el) {
        for(let i = 0; i < this.balls.length; i++) {
          let ball = this.balls[i]
          if(!ball.show) {
            ball.show = true
            ball.el = el
            this.dropBalls.push(ball)
            return
          }
        }
      },
      toggleList() {
        if(!this.totalCount) {
          return
        }
        this.fold = !this.fold
      },
      hideList() {
        this.fold = true
      },
      checkout() {
        if(this.totalPrice < this.minPrice) {
          return
        }
        window.alert(`支付了${this.totalPrice}元`)
        this.empty()
      },
      empty() {
        this.selectFoods.forEach((food) => {
          food.count = 0
        })
      }
    },
    transitions: {
      drop: {
        beforeEnter(el) {
          let count = this.balls.length
          while(count--) {
            let ball = this.balls[count]
            if(ball.show) {
              let rect = ball.el.getBoundingClientRect()
              let x = rect.left - 32
              let y = -(window.innerHeight - rect.top - 22)
              
              el.style.display = ''
              el.style.webkitTransform = `translate3d(0, ${y}px, 0)`
              el.style.transform = `translate3d(0, ${y}px, 0)`

              let inner = el.getElementsByClassName('inner-hook')[0]
              inner.style.webkitTransform = `translate3d(${x}px, 0, 0)`
              inner.style.transform = `translate3d(${x}px, 0, 0)`
            }
          }
        },
        enter(el) {
          /* eslint-disable no-unused-vars */
          let rf = el.offsetHeight
          this.$nextTick(() => {
            el.style.display = ''
            el.style.webkitTransform = 'translate3d(0, 0, 0)'
            el.style.transform = 'translate3d(0, 0, 0)'

            let inner = el.getElementsByClassName('inner-hook')[0]
            inner.style.webkitTransform = 'translate3d(0, 0, 0)'
            inner.style.transform = 'translate3d(0, 0, 0)'
          })
        },
        afterEnter(el) {
          let ball = this.dropBalls.shift()
          if (ball) {
            ball.show = false
            el.style.display = 'none'
          }
        }

      }
    },
    components: {
      Controll
    }
  }
</script>
<style lang="stylus" ref="stylesheet/stylus">
  @import '../../assets/style/minxin.stylus'
  .shopcart
    position: fixed
    bottom: 0
    left: 0
    width: 100%
    height: 48px
    z-index: 100
    background-color: #141d27
    .content
      display: flex
      .content-left
        flex: 1
        font-size: 0
        height: 100%
        .icon-wrap, .desc, .price
          display: inline-block
          vertical-align: top
        .icon-wrap
          position: relative
          top: -10px
          width: 50px
          height: 50px
          padding: 6px
          margin: 0 6px 0 12px
          border-radius: 50%
          box-sizing: border-box
          background-color: #141d27
          .cart-num
            position: absolute
            top: 0
            right: 0
            width: 24px
            height: 16px
            line-height: 16px
            font-size: 9px
            font-weight: 700
            border-radius: 15px
            box-shadow: 0 4px 8px 0 rgba(0,0,0,.4)
            background-color: rgb(240, 20, 20)
            color: #fff
            text-align: center
          .icon
            width: 100%
            height: 100%
            border-radius: 50%
            background-color: #2b343c
            text-align: center
            &.highlight
              color: #fff
              background-color: rgb(0, 160, 220)
              i
                color: rgb(255, 255, 255)
              .price
                color: rgb(255, 255, 255)
            i
              font-size: 24px
              line-height: 38px
              color: rgba(255, 255, 255, .6)
        .price,.desc
          margin-top: 12px
          font-size: 14px
          line-height: 24px
          color: rgba(255, 255, 255, .4)
        .price
          padding-right: 12px
          border-right: 1px solid rgba(255, 255, 255, .1)
          &.highlight
            color: #fff
        .desc
          padding-left: 12px
      .content-right
        flex: 0 0 95px
        width: 95px
        .pay
          padding: 0 8px
          line-height: 48px
          font-size: 12px
          font-weight: 700
          color: rgba(255, 255, 255, .4)
          background-color: #2b343c
          text-align: center
          &.enough
            background-color: #00b43c
            color: #fff
    .ball-container
      .ball
        position: fixed
        left: 30px
        bottom: 24px
        z-index: 99
        &.drop-transition
          transition: all .4s cubic-bezier(0.49, -0.29, 0.75, 0.41)
          .inner
            width: 16px
            height: 16px
            border-radius: 50%
            background-color: rgb(0, 160, 220)
            transition: all .4s linear
    .shopcart-list
      position: absolute
      top: 0
      left: 0
      z-index: -2
      width: 100%
      &.fold-transition
        transition: all .5s
        transform: translate3d(0, -100%, 0)
      &.fold-enter, &.fold-leave
        opacity: 0
        transition: all .5s
        transform: translate3d(0, 0, 0)
      .list-header
        height: 40px
        line-height: 40px
        padding: 0 18px
        background-color: #f3f5f7
        border-bottom: 1px solid rgba(7, 17, 27, .1)
        .title
          font-size: 14px
          color: rgb(7, 17, 27)
          float: left
        .empty
          font-size: 12px
          color: rgb(0, 160, 220)
          float: right
      .list-content
        max-height: 217px
        padding: 0 18px
        overflow: hidden
        background-color: #fff
        .food
          position: relative
          height: 48px
          border-1px: rgba(7, 17, 27, .1)
          .name
            font-size: 14px
            color: rgb(7, 17, 27)
            line-height: 48px
          .price
            position: absolute
            right: 90px
            top: 12px
            line-height: 24px
            font-size: 14px
            font-weight: 700
            color: rgb(240, 20, 20)
            margin-left: 18px
            margin-right: 12px
          .cart-ctrl-wrapper
            position: absolute
            right: 0
            top: 12px
  .list-mask
    position: fixed
    left: 0
    top: 0
    width: 100%
    height: 100%
    z-index: 20
    backdrop-filter: blur(10px)
    &.fade-transition
      transition: all .5s
      opacity: 1
      background-color: rgba(7, 17, 27, .6)
    &.fade-enter, &.fade-leave
      transition: all .5s
      opacity: 0
      background-color: rgba(7, 17, 27, 0)
</style>